<template>
  <div class="app-container">
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-input
          v-model="query.projectName"
          clearable
          size="small"
          placeholder="请输入项目名称"
          style="width: 200px;"
          class="filter-item"
        />
        <rrOperation />
      </div>
      <div class="btnContain">
        <el-button @click="addProject">新建项目</el-button>
      </div>
    </div>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" border :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <el-table-column :show-overflow-tooltip="true" prop="projectName" label="项目名称" />
      <el-table-column :show-overflow-tooltip="true" prop="maxEarnings" label="最高收益">
        <template slot-scope="scope">
          <el-dropdown placement="bottom" @command="handleCommand(scope.row)">
            <span class="el-dropdown-link">
              {{ scope.row.maxEarnings }}
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="a">编辑</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="bookListUploadTime" label="书单上传-更新时间">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="openDialog(scope.row)">上传</el-button>
          <el-button v-if="scope.row.bookListUploadTime" type="text" size="small" @click="downLoadBooks(scope.row.projectId)">下载</el-button>
          <span v-if="scope.row.bookListUploadTime">{{ scope.row.bookListUploadTime }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="bookListLinkUpdateTime" label="书单链接-更新时间">
        <template slot-scope="scope">
          <span v-if="scope.row.bookListLinkUpdateTime">{{ scope.row.bookListLinkUpdateTime }}</span>
          <el-button type="text" size="small" @click="update(scope.row)">更新</el-button>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="projectSorting" label="排序">
        <template slot-scope="scope">
          <span>{{ scope.row.projectSorting }}</span>
          <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="教程">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="upProject(scope.row)">更新</el-button>
          <!-- <el-button type="text" size="small" @click="deleDoc(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="添加时间" align="center" prop="createdAt" />
      <el-table-column
        label="操作"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="upProject(scope.row)">修改</el-button>
          <el-button v-if="scope.row.projectStatus !== 'hidden'" type="text" size="small" @click="stateChange('隐藏',scope.row)">隐藏</el-button>
          <el-button v-if="scope.row.projectStatus === 'hidden'" type="text" size="small" @click="stateChange('恢复',scope.row)">恢复</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination />
    <el-dialog :visible.sync="dialog" title="上传书单" :close-on-click-modal="false" append-to-body width="500px">
      <!-- <el-upload
        :on-success="handleSuccess"
        :limit="1"
        :data="uploadObjs"
        :before-upload="onBeforeUpload"
        :file-list="fileList"
        :headers="headers"
        :action="baseApi+'/api/book/import'"
      >
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleUpload">确认</el-button>
      </div> -->

      <el-upload
        :on-change="(file, fileList) => { return fileChange(file, fileList)}"
        :file-list="fileList"
        :auto-upload="false"
        :show-file-list="false"
      >
        <el-button size="mini">上传</el-button>
      </el-upload>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleUpload">确认</el-button>
      </div> -->
    </el-dialog>
    <el-dialog
      title="排序"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-input v-model="number" type="number" :min="0" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editNumber">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="最大收益"
      :visible.sync="dialogCVisible"
      width="30%"
    >
      <el-input v-model="max" type="number" :min="0" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogCVisible = false">取 消</el-button>
        <el-button type="primary" @click="editMax">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="书单链接"
      :visible.sync="dialogBookVisible"
      width="30%"
    >
      <el-input v-model="bookListLink" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogBookVisible = false">取 消</el-button>
        <el-button type="primary" @click="editBook">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import crudProject from '@/api/project'
import { show, hide, editProject, upload, deleTs } from '@/api/project'
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import { downloadFile } from '@/utils'
import pagination from '@crud/Pagination'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'
export default {
  name: 'Project',
  components: { rrOperation, pagination },
  cruds() {
    return CRUD({ title: '项目', url: 'api/project', crudMethod: { ...crudProject }})
  },
  mixins: [presenter(), header(), crud()],
  data() {
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      dialogVisible: false,
      dialogBookVisible: false,
      headers: {
        'Authorization': getToken()
      },
      dialog: false,
      info: {},
      number: 0,
      projectId: '',
      dialogCVisible: false,
      ruleForm: {},
      fileList: [],
      file: [],
      bookList: [],
      max: 0,
      bookListLink: ''
    }
  },
  computed: {
    ...mapGetters([
      'user',
      'baseApi'
    ])
  },
  created() {
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  methods: {
    handleCommand(row) {
      this.info = row
      this.max = row.maxEarnings
      this.dialogCVisible = true
    },
    openDialog(row) {
      this.dialog = true
      this.projectId = row.projectId
    },
    downLoadBooks(projectId) {
      crudProject.downLoadBook({ projectId: projectId }).then((res) => {
        downloadFile(res, '书单', 'xlsx')
        this.crud.refresh()
      })
    },
    fileChange(file, fileList) {
      this.fileList = fileList.slice(-3)
      this.handleUpload(file, fileList)
    },
    handleUpload(file, fileList, id) {
      const data = new FormData()
      data.append('file', file.raw)
      data.append('projectId', this.projectId)
      upload(data).then(response => {
        this.$message.success('上传成功！')
        this.dialog = false
      })
    },
    edit(row) {
      this.info = row
      this.number = row.projectSorting
      this.dialogVisible = true
    },
    doSubmit() {
      this.bookList.forEach((item) => {
        item.projectId = this.projectId
      })
      this.info.bookList = this.bookList
      if (this.bookList.length === 0) {
        this.$message.warning('请上传书单！')
      } else {
        editProject(this.info).then((res) => {
          this.bookList = []
          this.dialog = false
          this.crud.toQuery()
        })
      }
    },
    deleDoc(row) {
      deleTs([row.tutorial.tutorialId]).then((res) => {
        this.$message.success('删除成功！')
        this.crud.toQuery()
      }).catch(() => {
        this.$message.error('出错啦！')
      })
    },
    handleSuccess(response, file, fileList) {
      console.log(file, fileList)
    },
    editNumber() {
      this.info.projectSorting = this.number
      editProject(this.info).then((res) => {
        this.crud.toQuery()
        this.dialogVisible = false
      })
    },
    editMax() {
      this.info.maxEarnings = this.max
      editProject(this.info).then((res) => {
        this.crud.toQuery()
        this.dialogCVisible = false
      })
    },
    update(row) {
      this.info = row
      this.bookListLink = row.bookListLink
      this.dialogBookVisible = true
    },
    editBook() {
      this.info.bookListLink = this.bookListLink
      editProject(this.info).then((res) => {
        this.crud.toQuery()
        this.dialogBookVisible = false
      })
    },
    stateChange(index, row) {
      this.$confirm(`确定${index}吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        if (index === '隐藏') {
          await hide({
            id: row.projectId
          })
        } else if (index === '恢复') {
          await show({
            id: row.projectId
          })
        }
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
        this.crud.toQuery()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    addProject() {
      this.$router.push({
        path: '/project/addProject/index'
      })
    },
    upProject(row) {
      this.$router.push({
        path: '/project/addProject/index',
        query: {
          projectId: row.projectId
        }
      })
    }
  }
}
</script>

    <style rel="stylesheet/scss" lang="scss" scoped>
      .head-container {
        display: flex;
        justify-content: space-between;
        .btnContain {
          ::v-deep .el-button {
          background-color: rgba(236, 91, 91, 1);
          color: #fff;
          border: 0;
        }
        }
      }

    </style>

